/**
	 * @author Franklin Pearson
	 * @section 003
	 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;


public class Minefield extends JFrame implements ActionListener {

	
	/**
	 * Attributes
	 */
	private JButton addButton;
	public JButton [][] buttonField;
	private Minefield minefield;
	int cursorRow;
	int cursorColumn;
	int startRow;
	int startColumn;
	int finishRow;
	int finishColumn;
	Color DefaultColor;

	/**
	 * Constructor
	 */
    public Minefield(){
    	super("Mine Field");
    	
        this.setSize(600,550);
        
        buttonField = new JButton[5][5];
        this.setup();
        
        
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    	super.setVisible(true);
    }
	/**
	 * Setup the panel
	 */
    public void setup(){
    	
    	Random random = new Random();
    	super.setLayout(null);
		
		//change background color
		super.setBackground(new Color(50,200,100));
		super.setForeground(new Color(0, 0, 0));
		
		//Creating the buttons
		for(int i=0; i<5; i++){
			for(int j=0; j<5; j++){
				this.buttonField[i][j] = new JButton();
				this.buttonField[i][j].setBounds(100+(i*80),20+(j*80),80,80);
				this.buttonField[i][j].setActionCommand(i+","+j);
				this.buttonField[i][j].addActionListener(this);
				super.add(this.buttonField[i][j]);
				DefaultColor=this.buttonField[i][j].getBackground();
				
			}
		}
		//The clear button
		this.addButton = new JButton("Clear");
		this.addButton.setBounds(180, 420, 120, 40);
		this.addButton.setActionCommand("clear");
		this.addButton.addActionListener(this);
		super.add(addButton);
		
		//The Done button
		this.addButton = new JButton("Done");
		this.addButton.setBounds(300, 420, 120, 40);
		this.addButton.setActionCommand("done");
		this.addButton.addActionListener(this);
		super.add(addButton);
		
		//The start button
		startRow=random.nextInt(5);
		cursorRow=startRow;
		startColumn=random.nextInt(5);
		cursorColumn=startColumn;
		this.buttonField[startRow][startColumn].setBackground(Color.BLACK);
		this.buttonField[startRow][startColumn].setForeground(Color.WHITE);
		this.buttonField[startRow][startColumn].setText("Start");
		
		//the finish button
		int row1=random.nextInt(5);
		int column1=random.nextInt(5);
		boolean a = true;
		while(a){
		if(startRow!=row1 && startColumn!=column1){
		this.buttonField[row1][column1].setBackground(Color.BLACK);
		this.buttonField[row1][column1].setForeground(Color.WHITE);
		this.buttonField[row1][column1].setText("Finish");
		finishRow=row1;
		finishColumn=column1;
		a=false;
		}else{
			row1=random.nextInt(5);
			column1=random.nextInt(5);
		}
		}
		
	
    }
    
 
    
    
    /**
     * The Clear method, resets the game 
     */
    public void Clear(){
    	
    	//sets all buttons back to default
    	for(int i=0; i<5; i++){
			for(int j=0; j<5; j++){
				buttonField[i][j].setBackground(DefaultColor);
				buttonField[i][j].setText("");
				buttonField[i][j].setActionCommand(i+","+j);
				this.buttonField[i][j].addActionListener(this);
				
				
			}
    	}
    	// sets the other buttons, same as setup method
    	Random random = new Random();
    	startRow=random.nextInt(5);
		cursorRow=startRow;
		startColumn=random.nextInt(5);
		cursorColumn=startColumn;
		this.buttonField[startRow][startColumn].setBackground(Color.BLACK);
		this.buttonField[startRow][startColumn].setForeground(Color.WHITE);
		this.buttonField[startRow][startColumn].setText("Start");
		
		int row1=random.nextInt(5);
		int column1=random.nextInt(5);
		boolean a = true;
		while(a){
			
		if(startRow!=row1 && startColumn!=column1){
		this.buttonField[row1][column1].setBackground(Color.BLACK);
		this.buttonField[row1][column1].setForeground(Color.WHITE);
		this.buttonField[row1][column1].setText("Finish");
		finishRow=row1;
		finishColumn=column1;
		a=false;
		}else{
			row1=random.nextInt(5);
			column1=random.nextInt(5);
		}
		}
    
    	
    }
    
    /**
     * Method Done determines if the game has been won or lost and displays the appropriate message
     * and reveals the location of the mines
     */
    public void Done(){
    	Random random = new Random();
    	boolean isValid=false;
    	int row = 0;
    	int column = 0;
    	
    	int row1 = 0;
    	int column1 = 0;
    	//sets the place of the mines making sure they are not put on the start or finish
    	while(!isValid){
    		row=random.nextInt(5);
    		column=random.nextInt(5);
    		
    		row1=random.nextInt(5);
    		column1=random.nextInt(5);
    		if((row==startRow && column==startColumn)|| (row1==startRow&& column1==startColumn) || 
    				(row==finishRow && column==finishColumn) || (row1==finishRow && column1==finishColumn)){
    			
    		}
    		else{
    			isValid=true;
    		}
    	}
    	
	
		boolean won=true;
		
		//loops through mineField and checks to see if you path has crossed through a mine
		for(int i=0; i<5; i++){
			for(int j=0; j<5; j++){
				if(this.buttonField[i][j].getBackground().equals(Color.BLACK)){
					if((i==row || i==row1) && (j==column || j==column1) ){
						won=false;
					}else{
						
					}
				}
			}
		}
	
		//Making sure you finish the game,(i.e: there is a complete path to the finish)
		if(finishRow+1!=5 && this.buttonField[finishRow+1][finishColumn].getBackground().equals(Color.BLACK)){
			
		}else if(finishRow-1!=-1 && this.buttonField[finishRow-1][finishColumn].getBackground().equals(Color.BLACK)){
			
		}else if(finishColumn+1!=5 && this.buttonField[finishRow][finishColumn+1].getBackground().equals(Color.BLACK)){
			
		}else if(finishColumn-1!=-1 && this.buttonField[finishRow][finishColumn-1].getBackground().equals(Color.BLACK)){
			
		}
		else{
			JOptionPane.showMessageDialog(null, "Nice Try, you have to actually finish.");
			return;
		}
		//creating the mines
		this.buttonField[row][column].setText("Mine");
		this.buttonField[row][column].setBackground(Color.GREEN);
		
		this.buttonField[row1][column1].setText("Mine");
		this.buttonField[row1][column1].setBackground(Color.GREEN);
		
		
		
    	if(won){
    		JOptionPane.showMessageDialog(null, "You Win!!");
    		
    	}
    	else{
    		JOptionPane.showMessageDialog(null, "You Lose!!");
    		
    	}
    }
    


	/*
	 * What happens when an event in performed
	 */
	public void actionPerformed(final ActionEvent event) {
		
		String command = event.getActionCommand();
		String text; 
    	int row=0;
    	int column=0;
    	//if the button is anything other than mineField does appropriate action
    	if(command.equals("clear") || command.equals("done")){
    		if(command.equals("clear")){
    			
    			this.Clear();
    		}
    		if(command.equals("done")){
    			this.Done();
    		}
    		
    	}
    	//if a mine button is pressed 
    	else{
    	String[] yx=event.getActionCommand().split(",");
    	row	=Integer.parseInt(yx[0]);
    	column=Integer.parseInt(yx[1]);
		
    	//checks to see your move is legal
		for(int i=0; i<5; i++ ){
			for(int j=0; j<5; j++){
				if(row==i && column==j){
					if(Math.abs(cursorRow-row)+Math.abs(cursorColumn-column)>1){
						
					}else{
					this.buttonField[i][j].setBackground(Color.BLACK);
					cursorRow=row;
			    	cursorColumn=column;
					}
				}
			}
		}
	
    	}
	}
	
    /*
     * The Main method
     */
	public static void main(final String[] args) {
		final Minefield theGame = new Minefield();

	}

}

